package com.jiangjiang.eshop.logistics.calculator;

import com.alibaba.fastjson.JSONObject;
import com.jiangjiang.eshop.logistics.dto.FreightTemplateDTO;
import com.jiangjiang.eshop.order.dto.OrderInfoDTO;
import com.jiangjiang.eshop.order.dto.OrderItemDTO;
import org.springframework.stereotype.Component;

/**
 * @ClassName ReachFreeFreightCalculator
 * @Description TODO
 * @Author jhw
 * @Date 2021/9/20 22:23
 * @Version 1.0
 **/
@Component
public class ReachFreeFreightCalculator implements FreightCalculator{
    /**
     * 计算订单条目的运费
     * @param freightTemplate 运费模板
     * @param order 订单数据
     * @param orderItem 订单条目
     * @return
     */
    @Override
    public Double calculate(FreightTemplateDTO freightTemplate, OrderInfoDTO order, OrderItemDTO orderItem) {

        JSONObject rule = JSONObject.parseObject(freightTemplate.getRule());
        Double threshold = rule.getDoubleValue("threshold");
        Double lessThanThresholdFreight = rule.getDoubleValue("less_than_threshold_freight");
        Double totalAmount = orderItem.getPurchaseQuantity() * orderItem.getPurchasePrice();
        if(totalAmount >= threshold){
            return 0.0;
        } else {
            return lessThanThresholdFreight;
        }

    }
}
